package com.hutiao.snowhb.actions.wechat;

import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.jdom.JDOMException;

import com.hutiao.snowhb.common.SimsimiClient;
import com.hutiao.snowhb.wechat.tools.WeiXinTools;
import com.hutiao.snowhb.wechat.vo.WxRecvEventClick;
import com.hutiao.snowhb.wechat.vo.WxRecvEventSubscribe;
import com.hutiao.snowhb.wechat.vo.WxRecvEventUnsubscribe;
import com.hutiao.snowhb.wechat.vo.WxRecvGeoMsg;
import com.hutiao.snowhb.wechat.vo.WxRecvLinkMsg;
import com.hutiao.snowhb.wechat.vo.WxRecvMsg;
import com.hutiao.snowhb.wechat.vo.WxRecvPicMsg;
import com.hutiao.snowhb.wechat.vo.WxRecvVideoMsg;
import com.hutiao.snowhb.wechat.vo.WxRecvVoiceMsg;
import com.hutiao.snowhb.wechat.vo.WxRecvTextMsg;
import com.hutiao.snowhb.wechat.vo.WxSendNewsMsg;

import com.hutiao.snowhb.wechat.vo.WxSendMsg;
import com.hutiao.snowhb.wechat.vo.WxSendTextMsg;

import com.opensymphony.xwork2.ActionSupport;

@Namespace("/wechat")
public class WechatAction extends ActionSupport {
	
	private static Logger logger = Logger.getLogger(WechatAction.class);
	
	private static final long serialVersionUID = -2413791342162564357L;
	
	private String result = "";

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	@Action(value="index", results = { 
			@Result(name="rn_check", type="chain", location="check"), 
			@Result(name="success",type="xmlstr", params={"stringName", "result"})})
	public String execute() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest ();
		String method = ServletActionContext.getRequest().getMethod().toUpperCase();
		logger.info(method);
		
		if (method.equals("GET")) {
			// 处理验证URL请求
			
			
			Enumeration en = request.getParameterNames();
			while (en.hasMoreElements()) {
				String paramName = (String) en.nextElement();
				logger.info(paramName + " = " + request.getParameter(paramName));
			}
			
			
			return "rn_check";
		}
		
		if (!method.equals("POST")) {
			// 不处理非GET、非POST请求
			this.result = "";
			return SUCCESS;
		}
		
		String wxMsgXml = IOUtils.toString(request.getInputStream(),"utf-8");
		logger.info(wxMsgXml);
		//WxRecvMsg msg = WeiXinTools.recv(request.getInputStream());
		WxRecvMsg msg = WeiXinTools.recv(wxMsgXml);
		
		if (msg instanceof WxRecvTextMsg) {
			logger.info("msg type is WxRecvTextMsg");
			return this.handleTextMsg(msg);
		} else if (msg instanceof WxRecvPicMsg) {
			
		} else if (msg instanceof WxRecvVoiceMsg) {
			
		} else if (msg instanceof WxRecvVideoMsg) {
			
		} else if (msg instanceof WxRecvGeoMsg) {
			
		} else if (msg instanceof WxRecvLinkMsg) {
			
		} else if (msg instanceof WxRecvEventSubscribe) {	// 关注事件,扫描二维码事件
			logger.info("msg type is WxRecvEventSubscribe");
			return this.handleEvent(msg);
		} else if (msg instanceof WxRecvEventClick) {
			logger.info("msg type is WxRecvEventClick");
			return this.handleClickEvent(msg);
		}
		
		//logger.info(msg);
		
		return SUCCESS;
	}
	
	/**
	 * 处理文本消息
	 * @param msg
	 * @return
	 * @throws JDOMException
	 * @throws IOException
	 */
	public String handleTextMsg(WxRecvMsg msg) throws JDOMException, IOException {
		WxSendMsg sendMsg = WeiXinTools.builderSendByRecv(msg);
		WxRecvTextMsg recvMsg = (WxRecvTextMsg) msg;
		
		// 用户输入的内容
		String text = recvMsg.getContent().trim();
		logger.info("text:"+text);
		
		if (text.equals("笑话") || text.equals("1")) {
			// 文本消息回复
			sendMsg = new WxSendTextMsg(sendMsg, "需求已经确定了");
			this.result = WeiXinTools.getSendXmlStr(sendMsg);
		} else if (text.equals("图文") || text.equals("2")) {
			// 图文消息回复
			
			logger.info("tuwenxiaoxi test2324");
			WxSendNewsMsg sendNewsMsg = new WxSendNewsMsg(sendMsg);
			
			sendNewsMsg.addItem("雪花 勇闯天涯", "顶级品质，至臻时尚的雪花脸谱、优雅大方，纯净奢华的雪花晶尊、尊贵沉稳，匠心凝聚的雪花纯生、清新爽口，充满活力的勇闯天涯、麦香浓郁的雪花原汁麦、口感纯正的雪花清纯", "http://static.hutiao.me/xuehua/xuehuapinpai/images/1.jpg", "http://static.hutiao.me/xuehua/xuehuapinpai/xuanchuan/xuanchuan.html");
			
			this.result = WeiXinTools.getSendXmlStr(sendNewsMsg);
		} else {
			// simsimi机器人处理
			String sss = SimsimiClient.chat(text);
			sendMsg = new WxSendTextMsg(sendMsg, sss);
			this.result = WeiXinTools.getSendXmlStr(sendMsg);
		}
		
		return SUCCESS;
	}
	
	
	/**
	 * 处理关注/取消关注事件
	 * @param msg
	 * @return
	 * @throws JDOMException
	 * @throws IOException
	 */
	public String handleEvent(WxRecvMsg msg) throws JDOMException, IOException {
		WxSendMsg sendMsg = WeiXinTools.builderSendByRecv(msg);
		WxRecvEventSubscribe subsEvent = (WxRecvEventSubscribe) msg;
		
		if (subsEvent.getEvent().equals("subscribe")) {
			//sendMsg = new WxSendTextMsg(sendMsg, "欢迎关注雪花啤酒湖北公众账号！");
			//this.result = WeiXinTools.getSendXmlStr(sendMsg);
			
//			//logger.info("tuwenxiaoxi test");
//			WxSendNewsMsg sendNewsMsg = new WxSendNewsMsg(sendMsg);
//			sendNewsMsg.addItem("雪花 勇闯天涯", 
//					"顶级品质，至臻时尚的雪花脸谱、优雅大方，纯净奢华的雪花晶尊、尊贵沉稳，匠心凝聚的雪花纯生、清新爽口，充满活力的勇闯天涯、麦香浓郁的雪花原汁麦、口感纯正的雪花清纯", 
//					"http://static.hutiao.me/xuehua/xuehuapinpai/images/1.jpg", 
//					"http://static.hutiao.me/xuehua/xuehuapinpai/xuanchuan/xuanchuan.html");
//			this.result = WeiXinTools.getSendXmlStr(sendNewsMsg);
			
			WxSendNewsMsg sendNewsMsg = new WxSendNewsMsg(sendMsg);
			sendNewsMsg.addItem("雪花中国", 
					"雪花啤酒自2006年起，连续7年全国销量第一。在2013年《中国500最具价值品牌》中，“雪花”品牌价值达735.36亿元，与茅台、五粮液等名列前茅。", 
					"http://static.hutiao.me/snow/sy_images/xhzg.jpg", 
					"http://static.hutiao.me/snow/xhpp/1xhzg/xhzg.html");
			this.result = WeiXinTools.getSendXmlStr(sendNewsMsg);
			
			
		} else if (subsEvent.getEvent().equals("scan")) {
			// 处理扫描二维码，同时已经是关注用户了
		}
		
		return SUCCESS;
	}
	
	
	
	/**
	 * 处理菜单点击事件
	 * @param msg
	 * @return
	 * @throws JDOMException
	 * @throws IOException
	 */
	public String handleClickEvent(WxRecvMsg msg) throws JDOMException, IOException {
		WxSendMsg sendMsg = WeiXinTools.builderSendByRecv(msg);
		WxRecvEventClick clickEvent = (WxRecvEventClick) msg;
		
		if (clickEvent.getEventKey().equals("V1001_PINPAI_CHINA")) {
			// 品牌-雪花中国
			WxSendNewsMsg sendNewsMsg = new WxSendNewsMsg(sendMsg);
			sendNewsMsg.addItem("雪花中国", 
					"雪花啤酒自2006年起，连续7年全国销量第一。在2013年《中国500最具价值品牌》中，“雪花”品牌价值达735.36亿元，与茅台、五粮液等名列前茅。", 
					"http://static.hutiao.me/snow/sy_images/xhzg.jpg", 
					"http://static.hutiao.me/snow/xhpp/1xhzg/xhzg.html");
			this.result = WeiXinTools.getSendXmlStr(sendNewsMsg);
			
		} else if (clickEvent.getEventKey().equals("V1002_PINPAI_HUBEI")) {
			// 品牌-雪花湖北
			WxSendNewsMsg sendNewsMsg = new WxSendNewsMsg(sendMsg);
			sendNewsMsg.addItem("雪花湖北", 
					"雪花啤酒自2004年扎根湖北，十年间总投资超过25亿元，下设武汉、黄石、天门、宜昌4家工厂，年产能逾120万千升，是目前湖北省最大的啤酒企业。", 
					"http://static.hutiao.me/snow/sy_images/xhhb.jpg", 
					"http://static.hutiao.me/snow/xhpp/2xhhb/xhhb.html");
			this.result = WeiXinTools.getSendXmlStr(sendNewsMsg);
			
		} else if (clickEvent.getEventKey().equals("V1003_PINPAI_CHANPIN")) {
			// 品牌-产品
			WxSendNewsMsg sendNewsMsg = new WxSendNewsMsg(sendMsg);
			sendNewsMsg.addItem("雪花产品", 
					"雪花超高档脸谱系列，高档黑啤、晶尊、无醇、纯生系列，及银装、勇闯天涯、原汁麦、清纯、新动等诸多品种，均深受湖北消费者喜爱。", 
					"http://static.hutiao.me/snow/sy_images/xhcp.jpg", 
					"http://static.hutiao.me/snow/xhpp/3xhcp/mulu.html");
			this.result = WeiXinTools.getSendXmlStr(sendNewsMsg);
			
		} else if (clickEvent.getEventKey().equals("V1004_PINPAI_MORE")) {
			// 品牌-了解更多
			WxSendNewsMsg sendNewsMsg = new WxSendNewsMsg(sendMsg);
			sendNewsMsg.addItem("华润雪花官网", 
					"更多了解，详情请进雪花官网​。​", 
					"http://static.hutiao.me/snow/sy_images/xhgw.jpg", 
					"http://static.hutiao.me/snow/xhpp/4ljgd/ljgd.html");
			this.result = WeiXinTools.getSendXmlStr(sendNewsMsg);
			
		} else if (clickEvent.getEventKey().equals("V2001_HUODONG_SYDS")) {
			// 活动-全国摄影大赛
			WxSendNewsMsg sendNewsMsg = new WxSendNewsMsg(sendMsg);
			sendNewsMsg.addItem("雪花纯生.全国摄影大赛", 
					"雪花纯生匠心营造，中国古建筑摄影大赛2014光影园林已经拉开帷幕。", 
					"http://static.hutiao.me/snow/sy_images/csgj.jpg", 
					"http://www.archfans.com/index.html");
			this.result = WeiXinTools.getSendXmlStr(sendNewsMsg);
			
		} else if (clickEvent.getEventKey().equals("V2002_HUODONG_SSP")) {
			// 活动-随手拍
			sendMsg = new WxSendTextMsg(sendMsg, "正在建设中，敬请期待...");
			this.result = WeiXinTools.getSendXmlStr(sendMsg);
			
		}
		
		
//		else if (clickEvent.getEventKey().equals("V2001_HUODONG_GUJIAN")) {
//			// 活动-古建
//			WxSendNewsMsg sendNewsMsg = new WxSendNewsMsg(sendMsg);
//			sendNewsMsg.addItem("雪花纯生.古建筑摄影大赛活动", 
//					"当古典邂逅现代，当中国古建筑定格在镜头前，传承铸就了新的感动。雪花纯生•中国古建筑摄影大赛由华润雪花啤酒（中国）有限公司与清华大学建筑学院联合主办，经过五届的成功举办，目前已成为全国规模最大的中国古建筑类摄影赛事。大赛不仅为参赛者搭建了创作与分享摄影作品的平台，还通过线上互动、线下讲座、分享和交流等多种方式来支持摄影爱好者。活动期间，通过外拍活动，及专家点评、摄影讲座等方式，让参赛者亲身体验中国古建筑的韵味及中国文化的魅力。", 
//					"http://static.hutiao.me/snow/sy_images/csgj.jpg", 
//					"http://static.hutiao.me/snow/hdzq/1xhcssyds/cssymulu.html");
//			this.result = WeiXinTools.getSendXmlStr(sendNewsMsg);
//			
//		} else if (clickEvent.getEventKey().equals("V2002_HUODONG_YONGCHUANG")) {
//			// 活动-勇闯天涯
//			WxSendNewsMsg sendNewsMsg = new WxSendNewsMsg(sendMsg);
//			sendNewsMsg.addItem("雪花勇闯天涯活动介绍", 
//					"“勇闯天涯”，是华润雪花啤酒（中国）有限公司独立创新的年度原创性品牌推广活动。自2005年起，雪花啤酒勇闯天涯活动已连续成功举办九届，从雅鲁藏布大峡谷、探索长江源、远征国境线、极地探索、挑战乔戈里、勇攀长征之巅、穿越可可西里、冲破贡嘎雪线，到2013年翻越喜马拉雅，已共计行走10余万公里。活动本着“进取、挑战、创新”的核心精神，以其对人迹罕至的自然地理的独特挑战方式和对自然、人文环境的探访、科研、环保等举动，引起了业界的广泛关注和社会各界的高度评价，至今已成为目前全国持续时间最长、规模最大的原创户外品牌活动。", 
//					"http://static.hutiao.me/snow/sy_images/ycty.jpg", 
//					"http://static.hutiao.me/snow/hdzq/2xhycty/yctymulu.html");
//			this.result = WeiXinTools.getSendXmlStr(sendNewsMsg);
//			
//		} else if (clickEvent.getEventKey().equals("V2003_HUODONG_MAIZHI")) {
//			// 活动-原麦汁
//			WxSendNewsMsg sendNewsMsg = new WxSendNewsMsg(sendMsg);
//			sendNewsMsg.addItem("有麦香，真滋味——雪花原汁麦“特聘品酒师”招募活动", 
//					"2013年4月，华润雪花在湖北全省发起有麦香，真滋味——雪花原汁麦“特聘品酒师”招募活动，优胜者不仅能获得华润雪花“特聘品酒师”荣誉证书及八项独家特权，还将特邀参加华润雪花各种品酒活动，享有三年内免费畅饮雪花原汁麦特权。活动期间，华润雪花啤酒在武汉、赤壁、黄石、荆州、宜昌、襄阳、十堰等全省大部分地方，开辟了“有麦香，真滋味”雪花原汁麦特聘品酒师比赛的分赛场，累计超过20万人参与。", 
//					"http://static.hutiao.me/snow/sy_images/yzm.jpg", 
//					"http://static.hutiao.me/snow/hdzq/4xhyzm/yzmmulu.html");
//			this.result = WeiXinTools.getSendXmlStr(sendNewsMsg);
//			
//		} else if (clickEvent.getEventKey().equals("V2004_HUODONG_XINDONG")) {
//			// 活动-新动
//			WxSendNewsMsg sendNewsMsg = new WxSendNewsMsg(sendMsg);
//			sendNewsMsg.addItem("为新动而行动 iPhone5S等你拿", 
//					"“2013年12月15日起，消费者凡在湖北省荆州、洪湖、随州、监利、咸安、赤壁、汉川、黄石、蕲春、浠水、罗田、英山、武穴、黄梅购买雪花新动或雪花冰生产品，即可参与本次活动。​", 
//					"http://static.hutiao.me/snow/sy_images/xd.jpg", 
//					"http://static.hutiao.me/snow/hdzq/5xhxd/xd.html");
//			this.result = WeiXinTools.getSendXmlStr(sendNewsMsg);
//			
//		} else if (clickEvent.getEventKey().equals("V2005_HUODONG_HUANGSHI")) {
//			// 活动-雪花黄石工厂游
//			sendMsg = new WxSendTextMsg(sendMsg, "正在建设中，敬请期待...");
//			this.result = WeiXinTools.getSendXmlStr(sendMsg);
//		} 
		
		else if (clickEvent.getEventKey().equals("V3001_XUEHUAMEI_SINGER")) {
			// 雪花妹
			sendMsg = new WxSendTextMsg(sendMsg, "正在建设中，敬请期待...");
			this.result = WeiXinTools.getSendXmlStr(sendMsg);
		}
		
		return SUCCESS;
	}

}
